import Rect from '../elements/rect';
import * as _ from '../utils';
export default {
    bar: function (serie, opts) {
        let elements = [];
        let data = _.getObjProperty(serie, 'data', []);
        let origin = _.getObjProperty(opts, 'origin', {});
        let xAxis = _.getObjProperty(opts, 'xAxis', []);
        let yAxis = _.getObjProperty(opts, 'yAxis', {});
        let barSeriesNum = _.getObjProperty(opts, 'barSeriesNum', 1);
        let barSeriesIndex = _.getObjProperty(opts, 'barSeriesIndex', 1);

        let yAxisWidth = xAxis.yAxisWidth;

        let yAxisData = yAxis.data;
        let xAxisLen = yAxis.xAxisLen;

        let yAxisLen = yAxisData.length;
        let yMax = yAxisData[yAxisLen - 1];

        let x = origin.x;
        let y = origin.y;
        let w = origin.w;
        let h = origin.h;

        let offsetX = x + yAxisWidth;
        let offsetY = y;
        let color = serie.color;
        let dataLen = data.length;
        let xLen = parseInt((w - yAxisWidth) / xAxisLen);

        let allBarWidth = (xLen * 2) / 3;
        let allSpanWidth = xLen / 3;

        for (let i = 0; i < xAxisLen; i++) {
            if (i < dataLen) {
                let val = data[i];
                let p_w = allBarWidth / barSeriesNum;
                let p_h = (val * h) / yMax;
                let p_x =
                    offsetX +
                    xLen * i +
                    (allSpanWidth * (barSeriesIndex + 1)) / (barSeriesNum + 3) +
                    p_w * (barSeriesIndex - 0.5);
                let p_y = ((yMax - val / 2) * h) / yMax + offsetY;
                let style = {
                    fillStyle: color,
                    zIndex: 90,
                };
                let bar = new Rect({
                    points: [{ x: p_x, y: p_y, w: p_w, h: p_h }],
                    style,
                });
                elements.push(bar);
            }
        }

        return elements;
    },
};
